﻿using System;
using System.IO;
using System.Text;

namespace Dotnet.Utils.Utility.StreamUtil
{
    public class FileHelper
    {
        public string FileName
        {
            get
            {
                return this.sFileName;
            }
            set
            {
                this.sFileName = value;
            }
        }

        public FileHelper()
        {
        }

        public FileHelper(string FileName)
        {
            this.sFileName = FileName;
        }

        public string ReadFile()
        {
            string result;
            try
            {
                StreamReader streamReader = new StreamReader(this.sFileName, Encoding.Default);
                string text = streamReader.ReadToEnd();
                streamReader.Close();
                result = text;
            }
            catch
            {
                throw new Exception();
            }
            return result;
        }

        public void WriteFile(string sOldPath, string sNewPath)
        {
            try
            {
                FileInfo fileInfo = new FileInfo(sOldPath);
                fileInfo.CopyTo(sNewPath, true);
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }

        public static string FormatBytes(long bytes)
        {
            string[] Suffix = { "B", "KB", "MB", "GB", "TB" };
            int i;
            double dblSByte = bytes;
            for (i = 0; i < Suffix.Length && bytes >= 1024; i++, bytes /= 1024)
            {
                dblSByte = bytes / 1024.0;
            }
            return string.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
        }


        public static string FormatBytes2(long bytes)
        {
            string result;
            if ((double)bytes <= 999.0)
            {
                result = bytes.ToString() + " bytes";
            }
            else if ((double)bytes <= 1022976.0)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1024.0) + " KB";
            }
            else if ((double)bytes <= 1047527424.0)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1048576.0) + " MB";
            }
            else if ((double)bytes <= 1072668082176.0)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1073741824.0) + " GB";
            }
            else if ((double)bytes <= 1098412116148224.0)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1099511627776.0) + " TB";
            }
            else if ((double)bytes <= 1.1247740069357814E+18)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1125899906842624.0) + " PB";
            }
            else if ((double)bytes <= 1.1517685831022401E+21)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1.152921504606847E+18) + " EB";
            }
            else if ((double)bytes <= 1.1794110290966939E+24)
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1.1805916207174113E+21) + " ZB";
            }
            else
            {
                result = FileHelper.ThreeNonZeroDigits((double)bytes / 1.2089258196146292E+24) + " YB";
            }
            return result;
        }

        public static string ThreeNonZeroDigits(double value)
        {
            string result;
            if (value >= 100.0)
            {
                result = ((int)value).ToString();
            }
            else if (value >= 10.0)
            {
                result = value.ToString("0.0");
            }
            else
            {
                result = value.ToString("0.00");
            }
            return result;
        }

        private string sFileName = null;
    }
}